package com.acwebsitedesign.uRateDrugs.web;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.acwebsitedesign.uRateDrugs.domain.Drug;
import com.acwebsitedesign.uRateDrugs.domain.DrugService;
import com.acwebsitedesign.uRateDrugs.domain.DrugValidator;

@Controller
@SessionAttributes("addDrug")
public class AdminController {

	@Autowired
	DrugService drugService;

	@RequestMapping("/admin.html")
	public ModelAndView admin() {
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		if (!(username.contentEquals("admin"))){
			ModelAndView mav = new ModelAndView("redirect:uRateDrugs-webapp/spring_security_login");
			return mav;
		}
		
		ModelAndView mav = new ModelAndView("admin");
		return mav;
	}

	@RequestMapping("/addDrug.html")
	public ModelAndView addDrug() {
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		if (!(username.contentEquals("admin"))){
			ModelAndView mav = new ModelAndView("redirect:uRateDrugs-webapp/spring_security_login");
			return mav;
		}

		Drug addDrug = new Drug();
		ModelAndView mav = new ModelAndView("addDrug");
		mav.addObject("addDrug", addDrug);
		return mav;
	}

	@RequestMapping("/confirmDrug.html")
	public ModelAndView confirmDrug(@RequestParam String submit,
			@ModelAttribute("addDrug") Drug addDrug, BindingResult result)
			throws SQLException {
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		if (!(username.contentEquals("admin"))){
			ModelAndView mav = new ModelAndView("redirect:uRateDrugs-webapp/spring_security_login");
			return mav;
		}

		DrugValidator drugValidator = new DrugValidator();
		drugValidator.validate(addDrug, result);

		if (result.hasErrors()) {
			return new ModelAndView("addDrug");
		}

		ModelAndView mav = new ModelAndView("confirmDrug");
		mav.addObject("addDrug", addDrug);
		return mav;

	}

	@RequestMapping("/saveDrug.html")
	public ModelAndView saveDrug(@ModelAttribute("addDrug") Drug addDrug) {
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		if (!(username.contentEquals("admin"))){
			ModelAndView mav = new ModelAndView("redirect:uRateDrugs-webapp/spring_security_login");
			return mav;
		}

		drugService.saveDrug(addDrug);
		ModelAndView mav = new ModelAndView("saveDrug");
		return mav;
	}
	
	@RequestMapping("/getDrugID.html")
	public ModelAndView getDrugID() {
		ModelAndView mav = new ModelAndView("getDrugID");
		return mav;
	}
	
	@RequestMapping("/getDrugIDResults.html")
	public ModelAndView getDrugIDResults(@RequestParam String name) {

		List<Drug> drugList =drugService.getDrugByName(name);
		ModelAndView mav = new ModelAndView("getDrugIDResults");
		if (drugList.isEmpty()){
			return new ModelAndView("drugNotFound");	
		} else {
			mav.addObject(drugList);
		} 
		return mav;
	}

}